import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/LogIn.vue'
import Register from '../views/RegiSter.vue'
import AddProduct from '../views/AddProduct.vue'
import FavoRite from '../views/FavoRite.vue'
import ShopCart from '../views/ShopCart.vue'
import $store from '../store/index'
import ShopView from '../views/ShopView'
import ShowDetails from '../views/ShowDetails'
import ShopProduct from '../views/ShopProduct'
import MyOrder from '../views/MyOrder'
import UserList from '../views/UserList'
import UserInfo from '../views/UserInfo'
import ConnectView from '../views/ConnectView'
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      IsAuth: true,
    }
  },
  {
    path: '/login/',
    name: 'login',
    component: Login,
    meta: {
      IsAuth: false,
    }
  },
  {
    path: '/register/user/',
    name: 'register',
    component: Register,
    meta: {
      IsAuth: false,
    }
  },
  {
    path: '/addproduct/',
    name: 'addproduct',
    component: AddProduct,
    meta: {
      IsAuth: true,
    }
  },
  {
    path: '/favorite/',
    name: 'favorite',
    component: FavoRite,
    meta: {
      IsAuth: true,
    }
  },
  {
    path: '/shopcart/',
    name: 'shopcart',
    component: ShopCart,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/register/shop/',
    name: 'registershop',
    component: ShopView,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/showdetails/:id/',  //:id代表参数
    name: 'product_details',
    component: ShowDetails,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/shopproducts/:id/:name/',  //:id代表参数
    name: 'shop_products',
    component: ShopProduct,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/order/',
    name: 'order',
    component: MyOrder,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/userlist/',
    name: 'userlist',
    component: UserList,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/userinfo/',
    name: 'info',
    component: UserInfo,
    meta: {
      IsAuth: true,
    }
  },

  {
    path: '/connect/:username/',
    name: 'connect',
    component: ConnectView,
    meta: {
      IsAuth: true,
    }
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (!$store.state.is_login && to.meta.IsAuth) {
    next({ name: 'login' });
  } else { next(); }
})
export default router
